<template>
  <div class="phone-form">
    <el-form
      class="phone-form-container"
      :model="phoneForm"
      ref="phoneFormRef"
      :rules="phoneRules"
      :inline="false"
      label-width="80px"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="phoneForm.phone" class="public-input"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="verificationCode">
        <el-input
          v-model="phoneForm.verificationCode"
          input-style="width:200px"
          class="input-code public-input"
        >
          <template #append>
            <div class="code-box">
              <span class="text" @click="sendCode" v-if="!active">发送验证码</span>
              <!-- active为false代表正在倒计时 -->
              <span v-else :class="{ disabled: active }">{{ count }}后重新发送</span>
            </div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label-width="0px">
        <div class="loginBtn">
          <el-button type="primary" round @click="onSubmit(phoneFormRef)">登录</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import type { FormInstance } from 'element-plus'
import { correctCode, phoneRules } from '../config/phone-config'

const phoneFormRef = ref<FormInstance>()
const phoneForm = reactive({
  phone: '',
  verificationCode: '',
  correctCode //默认验证码
})

// 发送验证码
// const sendText = ref('发送验证码')
const active = ref(false) //false代表还没有点击发送验证码
const count = ref()
const timer: any = ref(null)
// 发送验证码倒计时
function sendCode() {
  if (phoneForm.phone !== '' && phoneForm.phone.length == 11) {
    count.value = 60
    active.value = true
    timer.value = setInterval(() => {
      if (active.value) {
        count.value -= 1
      }
      if (count.value <= 0) {
        console.log('小于等于0了')
        active.value = false
        clearInterval(timer.value)
      }
    }, 1000)
  } else {
    // 发送验证码之前校验是否
    phoneFormRef.value?.validateField('phone', (res) => {
      if (!res) return
    })
  }
}
// 登录
const emit = defineEmits(['submit'])
function onSubmit(formEl: FormInstance | undefined) {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      emit('submit', phoneForm)
      // 登录逻辑
    } else {
      return false
    }
  })
}
</script>

<style lang="less" scoped>
.phone-form {
  height: 100%;
  padding-top: 15px;
  .phone-form-container {
    width: 100%;
    height: 100%;
    /deep/.el-form-item {
      margin-bottom: 25px;
    }
    /deep/ .el-form-item__label {
      line-height: var(--formHeight);
    }
    .public-input {
      width: 300px;
      height: var(--formHeight);
    }
    .input-code {
      /deep/.el-input-group__append {
        height: var(--formHeight);
        background-color: #00a1d6;
        border-color: #00a1d6;
        border: 1px solid #dcdfe6;
        border-radius: 4px;
        margin-left: 5px;
        color: #fff;
        cursor: pointer;
        box-sizing: border-box;
      }
      .code-box {
        .disabled {
          color: #666;
        }
      }
    }

    .loginBtn {
      width: 100%;
      display: flex;
      justify-content: center;
    }

    /deep/.el-button.is-round {
      padding: 15px 30px;
    }
  }
}
</style>
